package unittest;

/**
 * 日期之间隔几天
 */
public class DaysBetweenDates {

    /**
     * 计算差距天数
     */
    public int compute(String date1, String date2) {
        return Math.abs(toDay(date1) - toDay(date2));
    }

    /**
     * 转换为天数
     */
    public int toDay(String dateStr) {
        String[] temp = dateStr.split("-");
        int year = Integer.parseInt(temp[0]);
        int month = Integer.parseInt(temp[1]);
        int day = Integer.valueOf(temp[2]);

        if (month <= 2) {
            year--;
            month += 10;
        } else month -= 2;
        return 365 * year + year / 4 - year / 100 + year / 400 + 30 * month + (3 * month - 1) / 5 + day /*- 584418*/;
    }
}
